package com.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration // gateway
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource corsConfigurationSource=  new UrlBasedCorsConfigurationSource();
    CorsConfiguration configuration=new CorsConfiguration() ;
    //配置跨域

        // 允许哪些头部请求跨域：全部
        configuration.addAllowedHeader("*");
        // 允许哪些请求方式跨域：所有
        configuration.addAllowedMethod("*");
        // 允许哪些请求来源跨域：任意
        configuration.addAllowedOrigin("*");
        // 是否允许携带cookie跨域：是
        configuration.setAllowCredentials(true);


        
      corsConfigurationSource.registerCorsConfiguration("/**",configuration);

        return new CorsWebFilter(corsConfigurationSource);
    }

}
